#pragma once
#include "character.h"

class EnemyAI : public Character
{
public:
	static const int MSG_TARGET = 1;

	void Update(float timeElapsed)
	{
		//error checking
		if (_target == NULL) return;

		m_secondsTillMove -= timeElapsed;
		if(m_secondsTillMove <= 0)
		{
			m_secondsTillMove += m_stdWaitTime;
			//Move towards target
			float deltaX = 0.0f;
			float deltaY = 0.0f;

			deltaX = (float)(_target->getVector2D().x - this->p.x);
			deltaY = (float)(_target->getVector2D().y - this->p.y);

			bool moved = false;
			if (deltaY < 0)
			{
				this->move('w');
				moved = true;
			}
			else if (deltaY > 0)
			{
				this->move('s');
				moved = true;
			}
			if(!moved)
			{
				if (deltaX < 0)
				{
					this->move('a');
				}
				else if (deltaX > 0)
				{
					this->move('d');
				}
			}
		}
	}

	void Recieve(int messageType, void* data)
	{
		// HACK Magic number
		if (messageType == 1)
		{
			_target = (Character*)data;
		}
	}

	EnemyAI(float a_secondsToWaitTillMove)
	{
		_target = NULL;
		m_stdWaitTime = a_secondsToWaitTillMove;
		m_secondsTillMove = m_stdWaitTime;
	}

private:
	//HACK if _target is every free'd then enemyai class could crash
	Character* _target;

	float m_stdWaitTime;
	float m_secondsTillMove;
};
